Ext.define('YX.ZbusRpc', {
	requires : ['Ext.Ajax'],
	singleton : true,
	timeout : 60000,
	module : '',
	_host : null,
	_topic : null,
	init : function(cfg) {
		if (cfg && cfg.host && cfg.topic) {
			this._host = cfg.host;
			if (!Ext.String.startsWith(this._host, "http:", true)) {
				this._host = "http://" + this._host;
			}
			if (!Ext.String.endsWith(this._host, "/", true)) {
				this._host = this._host + "/";
			}
			this._topic = cfg.topic;
		}
	},
	/**
	* 使用同Ext.Ajax 额外配置： options.module(后台服务名，字符串)
	* options.showFail(操作失败是否显示错误对话框，默认:true
	* options.showSuccess(操作成功是否显示成功对话框，默认:false)
	*/
	request : function(options) {
		options = options || {};
		options.url = options.module || options.url || me.module;
		var vToken = options.token || LoginInfo.TOKEN;

        if(options.showFail===null||options.showFail===""||options.showFail===undefined){
            options.showFail=true;
        }
        if(options.showSuccess===null||options.showSuccess===""||options.showSuccess===undefined){
            options.showSuccess=false;
        }
		var vShowFail = options.showFail;
		var vShowSuccess = options.showSuccess;

		if (options.token) {
			delete options.token;
		}

		if (!vToken) {
			vToken = "";
		}
		if (options.params) {
			options.jsonData = options.params;
			delete options.params;
		}

		options.headers = {
			"cmd" : "pub",
			"ack" : false,
			"mq" : this._topic,
			"Content-Type" : "application/json;charset=UTF-8",
			"module" : options.url,
			"requestToken" : vToken
		};
		options.url = this._host + options.url;
		options.method = "POST";

		if (vShowFail) {
			var failFun = options.failure;
			options.failure = function(rsp, opts) {
				if (Ext.isObject(rsp)) {
					Ext.Msg.show({
						  title : rsp.rspCode,
						  msg : rsp.rspDesc,
						  icon : Ext.Msg.ERROR,
						  buttons : Ext.Msg.OK
					  });
				} else {
					Ext.Msg.show({
						  title : "错误",
						  msg : rsp,
						  icon : Ext.Msg.ERROR,
						  buttons : Ext.Msg.OK
					  });
				}

				if (failFun) {
					Ext.callback(failFun, options.scope, [action.result, action]);
				}
			}
		}

		if (vShowSuccess) {
			var succFun = options.success;
			options.success = function(rsp, opts) {
				if (Ext.isObject(rsp)) {
					Ext.Msg.show({
						  title : rsp.rspCode,
						  msg : rsp.rspDesc,
						  icon : Ext.Msg.INFO,
						  buttons : Ext.Msg.OK
					  });
				}
				if (succFun) {
					Ext.callback(succFun, options.scope, [action.result, action]);
				}
			}
		}

		Ext.Ajax.cors = true;
		Ext.Ajax.request(options);
	},
	resolveFormRequest : function(formId) {
		var form = this.getForm({
			  form : formId
		  });
		var files = Ext.query("input[type=file]", form);
		if (Ext.isEmpty(files)) {
			return Ext.getCmp(formId).getValues();
		}

		var reader = new FileReader();
		var promise = new Promise(function(resolve, reject) {
			  reader.onload = function(e) {
				  if (!Ext.isEmpty(e.target.result)) {
					  var index = e.target.result.indexOf("base64,") + 7;
					  resolve(e.target.result.substr(index), true);
					  return;
				  };
				  resolve(e, true);
			  };
			  reader.onerror = function(e) {
				  reject(e, false);
			  }
		  });
		// 将文件以Data URL形式读入页面
		reader.readAsDataURL(files[0].files[0]);

		var rtn = {};
		rtn[files[0].name] = '', rtn[files[0].name + "FileName"] = files[0].files[0].name;

		return promise.then(function(data) {
			  rtn[files[0].name] = data;
			  return rtn;
		  }, function(data) {
			  return rtn;
		  });

		/*
		return promise.then((data,success) => {
			if(success){
				rtn[files[0].name]=data;
				return rtn;
			}else{
				throw rtn;
			} 
		      }).catch((e) => {});*/
	}
});
